﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfFlipit
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            string[] args = e.Args;
            var settings = FlipItSettings.Load();
            if (args.Length > 0)
            {
                string firstArgument = args[0].ToLower().Trim();
                string secondArgument = null;

                // Handle cases where arguments are separated by colon.
                // Examples: /c:1234567 or /P:1234567
                if (firstArgument.Length > 2)
                {
                    secondArgument = firstArgument.Substring(3).Trim();
                    firstArgument = firstArgument.Substring(0, 2);
                }
                else if (args.Length > 1)
                    secondArgument = args[1];

                if (firstArgument == "/c")
                {
                    SettingWindow settingWindow = new SettingWindow(settings);
                    settingWindow.Show();
                }
                else if (firstArgument == "/p")      // Preview mode
                {
                    if (secondArgument == null)
                    {
                        MessageBox.Show("抱歉，没有提供预期的窗口句柄。", "屏幕保护程序", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        IntPtr previewWndHandle = new IntPtr(long.Parse(secondArgument));
                        WpfFlipit.MainWindow mainWindow = new WpfFlipit.MainWindow(settings, previewWndHandle);
                        mainWindow.Show();
                    }
                }
                else if (firstArgument == "/s")      // Full-screen mode
                {
                    WpfFlipit.MainWindow mainWindow = new WpfFlipit.MainWindow(settings, SystemParameters.PrimaryScreenWidth, SystemParameters.PrimaryScreenHeight);
                    mainWindow.Show();
                }
                else    // Undefined argument
                {
                    MessageBox.Show("抱歉, 命令行参数 \"" + firstArgument +
                        "\" 无效.", "FlipIt", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                SettingWindow settingWindow = new SettingWindow(settings);
                settingWindow.Show();
            }
        }
    }
}
